package com.otojob.service.impl;

import com.otojob.entity.User;
import com.otojob.entity.vo.UserVo;
import com.otojob.mapper.UserMapper;
import com.otojob.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otojob.utils.JwtUtils;
import com.otojob.utils.Results;
import com.sun.corba.se.impl.legacy.connection.USLPort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TingFeng
 * @since 2022-03-18
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private HttpServletRequest request;

    /**
     * 修改电话
     * @param telephone
     * @return
     */
    @Override
    public Results updateUserTelephone(String telephone,int id) {
        String regex = "1\\d{10}";
        boolean matches = telephone.matches(regex);
        if(matches==false){
            return Results.error().message("电话号码不符合规则");
        }
        int i = 0;
        try {
            i=userMapper.updateUserTelephone(telephone,id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i==1){
            log.info("电话修改成功");
            return Results.ok();
        }
        log.info("电话修改失败");
        return Results.error();
    }

    /**
     * 修改昵称
     * @param nikename
     * @return
     */
    @Override
    public Results updateNikeName(String nikename,int id) {
        int i = 0;
        try {
            i=userMapper.updateNikeName(nikename,id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i==1){
            return Results.ok();
        }
        return Results.error();
    }
    /**
     * 修改头像路径
     * @param url
     * @return
     */
    @Override
    public Results updatePhoto(String url,int id) {


        int i = 0;
        try {
            i=userMapper.updatePhoto(url,id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i==1){
            return Results.ok();
        }
        return Results.error();
    }
    /**
     * 修改宣言
     * @param declaration
     * @return
     */
    @Override
    public Results updateDeclaration(String declaration,int id) {

        int i = 0;
        try {
            i=userMapper.updateDeclaration(declaration,id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i==1){
            return Results.ok();
        }
        return Results.error();
    }
    /**
     * 修改性别
     * @param sex
     * @return
     */
    @Override
    public Results updateUserSex(Integer sex,int id) {
        //默认为难
        String sex1 = "男";
        if(sex==1){
            sex1="女";
        }
        int i = 0;
        try {
            //判断是否修改成功
            i=userMapper.updateUserSex(sex1,id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i==1){
            return Results.ok();
        }
        return Results.error();
    }

    /**
     * 查询简历是否存在
     * @return
     */
    @Override
    public Results findMyResumeTF() {
        int id = JwtUtils.getMemberIdByJwtToken(request);

        int i = 0;
        try {
            i=userMapper.findMyResumeTF(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i==1){
            return Results.ok().data("findMyResume",1);
        }
        return Results.error().data("findMyResume",0);
    }
    /**
     * 添加简历url
     * @param url
     * @return
     */
    @Override
    public Results addResume(String url) {
        int id = JwtUtils.getMemberIdByJwtToken(request);
        int i = 0;
        try {
            i=userMapper.addResume(url,id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i==1){
            return Results.ok();
        }
        return Results.error();
    }
    /**
     * 删除简历
     * @return
     */
    @Override
    public Results deleteResume() {
        int id = JwtUtils.getMemberIdByJwtToken(request);
        int i = 0;
        try {
            i=userMapper.deleteResume(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i==1){
            return Results.ok();
        }
        return Results.error();
    }

    @Override
    public Results findUserById(int id) {
        User allById = userMapper.findAllById(id);
        if(allById==null){
            return Results.error();
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(allById,userVo);
        return Results.ok().data("user",userVo);
    }

    @Override
    public Results getResume() {
        int id = JwtUtils.getMemberIdByJwtToken(request);
        User allById = userMapper.findAllById(id);
        return Results.ok().data("url", allById.getResume())
                .data("nickname",allById.getNickname());
    }
}
